<template>
    <basic-container :btnFlag=true>
        <div class="card-border">
            <process-view-simple-register-flowsheet :taskId="taskId"/>
            <card-header :header-data="headerData"></card-header>
            <activiti-action ref="action"
                             :task-id="taskId"
                             @confirm="submitConfirm"
                             v-model="form">
                <template slot="buttonRight">
                    <web-office-button ref="webOfficeButton"
                                       v-model="form"
                                       type="create"
                                       button-text="拟稿"
                                       :listTitle="list.title"
                                       :listCode="list.code"></web-office-button>
                    <jointly-handle-btn v-if="taskInfo != null"
                                        type="JOINTLY_HANDLE_TYPE_LIST_FGW"
                                        :business-key="listId"
                                        :process-id="taskInfo.processId"
                                        :task-id="taskId">
                    </jointly-handle-btn>
                </template>
            </activiti-action>
            <el-tabs class="bs-tab" v-model="activeName"
                     @tab-click="tabClick"
                     type="card">
                <el-tab-pane label="报批信息"
                             name="first">
                    <bostech-detail :detail-data="listDetail"/>
                    <list-register-info :listId="listId"/>
                </el-tab-pane>
                <el-tab-pane label="批文"
                             name="webOffice">
                    <p>温馨提示：请在新打开的窗口中进行拟文、校对和盖章操作。如果无法进行在线编辑操作，请检查是否已经安装了WebOffice插件。</p>
                    <!--<div>-->
                    <!--<span style="padding-left: 3px;font-weight: 600">批文:</span>-->
                    <!--<div style="display: flex;justify-content: flex-start;align-items : center;">-->
                    <!--<div>-->
                    <!--<preview :attachementId="form.officeFileId" v-if="form.officeFileId" :key="previewShow"></preview>-->
                    <!--</div>-->
                    <!--<div v-if="form.officeFileId">-->
                    <!--<el-button type="primary" icon="el-icon-refresh" circle size="mini" @click="refreshPreview"></el-button>-->
                    <!--</div>-->
                    <!--</div>-->
                    <!--</div>-->
                </el-tab-pane>
                <el-tab-pane label="办理过程"
                             name="second">
                    <process-handle-history :task-id="taskId"/>
                </el-tab-pane>
                <el-tab-pane label="会办流程"
                             name="third">
                    <jointly-handle-div v-if="taskInfo != null && taskInfo.processId != null"
                                        type="JOINTLY_HANDLE_TYPE_LIST_FGW"
                                        :business-key="listId"
                                        :process-id="taskInfo.processId">
                    </jointly-handle-div>
                </el-tab-pane>
            </el-tabs>
        </div>
    </basic-container>
</template>

<script>
    import activitiAction from "@/components/activiti/action";
    import {getbasisinfo} from "@/api/planning/detailList";
    import listRegisterInfo from "@/components/list/listRegisterInfo"
    import processHandleHistory from "@/components/activiti/history/processHandleHistory";
    import {taskComplete, pageProcess, listProcessVariables, getTaskInfo} from "@/api/activiti/monitor";
    import webOfficeButton from "@/components/web-office/button";
    import {getfileinfo} from "@/api/system";
    import jointlyHandleBtn from "@/components/activiti/jointly-handle-button/jointlyHandleBtn";
    import {webOfficeCreateSpare} from "@/api/system";
    import * as WebCtrl from "../../../assets/WebOffice";
    import store from '@/store';
    import jointlyHandleDiv from "@/components/activiti/jointly-handle-button/jointlyHandleDiv";

    export default {
        name: "planningListFgwDo",
        components: {
            activitiAction,
            listRegisterInfo,
            processHandleHistory,
            webOfficeButton,
            jointlyHandleBtn,
            jointlyHandleDiv
        },
        data() {
            return {
                headerData: {
                    title: '主办经办拟稿'
                },
                activeName: 'first',
                taskId: null,
                listId: null,
                list: {
                    title: '',
                    code: null
                },
                listDetail: [],
                form: {
                    officeFileId: null
                },
                formRules: {},
                taskInfo: null,
                previewShow: '',
            };
        },
        created() {
            this.init();
        },
        watch: {
            'activeName'(val) {
                if (val == 'webOffice') {
                    if (this.form.officeFileId != null && this.form.officeFileId != '') {
                        this.toWebOfficeCheck()
                    } else {
                        this.toWebOfficeCreate()
                    }
                }
            }
        },
        methods: {
            init() {
                this.taskId = this.$route.params.taskId;
                this.listId = this.$route.query.listId;
                this.getList();
                this.getProcessVariables();
                this.getTaskInfo();
            },
            getTaskInfo() {
                getTaskInfo(this.taskId).then((res) => {
                    this.taskInfo = res.data.data;
                });
            },
            getList() {
                getbasisinfo(this.listId).then((res) => {
                    this.list = res.data.data;
                    let list = this.list;
                    if (list != null) {
                        this.listDetail = [
                            {label: '清单标题', value: list.title},
                            {label: '清单说明', value: list.description}
                        ]
                    }
                });
            },
            getProcessVariables() {
                listProcessVariables({taskId: this.taskId}).then((res) => {
                    let processVariables = res.data.data;
                    if (processVariables != null) {
                        this.form.officeFileId = processVariables.officeFileId;
                    }
                });
            },
            goBack() {
                this.$router.go(-1);
            },
            submitConfirm(validActionForm, stopLoading, closeDialog) {
                validActionForm((actionFormValid, object) => {
                    this.validateForm((valid) => {
                        if (valid && actionFormValid) {
                            let queryParam = {
                                filterByAdminRole: false,
                                current: 1,
                                size: -1,
                                needVariables: true,
                                ascs: 'start_time',
                                processKey: 'jointly-handle',
                                stringVariables: 'JOINTLY_HANDLE_PROCESS_ID#' + this.taskInfo.processId
                            };
                            //检查会办流程是否结束
                            pageProcess(queryParam)
                                .then(res => {
                                    let hasJointlyHandleProcessNotFinish = false;
                                    let data = res.data.data;
                                    let jointlyHandleProcessList = data.records;
                                    if (jointlyHandleProcessList != null && Array.isArray(jointlyHandleProcessList) && jointlyHandleProcessList.length > 0) {
                                        for (let process of jointlyHandleProcessList) {
                                            if (process.state != '1') {
                                                hasJointlyHandleProcessNotFinish = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (hasJointlyHandleProcessNotFinish) {
                                        this.$confirm('有会办流程尚未结束，结束当前任务，会结束相关的会办流程，是否继续?', '提示', {
                                            confirmButtonText: '确定',
                                            cancelButtonText: '取消',
                                            type: 'warning'
                                        }).then(() => {
                                            if (this.form.officeFileId == null) {
                                                this.officeFileIdConfirm(stopLoading, closeDialog)
                                            } else {
                                                //避免有文件id，但是用户没有保存文件
                                                getfileinfo(this.form.officeFileId).then((res) => {
                                                    let fileInfo = res.data.data;
                                                    if (fileInfo != null && fileInfo.size > 0) {
                                                        this.submit(stopLoading, closeDialog);
                                                    } else {
                                                        this.officeFileIdConfirm(stopLoading, closeDialog)
                                                    }
                                                });
                                            }
                                        }).catch(() => {
                                            stopLoading();
                                        });
                                    } else {
                                        if (this.form.officeFileId == null) {
                                            this.officeFileIdConfirm(stopLoading, closeDialog)
                                        } else {
                                            //避免有文件id，但是用户没有保存文件
                                            getfileinfo(this.form.officeFileId).then((res) => {
                                                let fileInfo = res.data.data;
                                                if (fileInfo != null && fileInfo.size > 0) {
                                                    this.submit(stopLoading, closeDialog);
                                                } else {
                                                    this.officeFileIdConfirm(stopLoading, closeDialog)
                                                }
                                            });
                                        }
                                    }
                                });
                        } else {
                            this.formValidateErrorNotify();
                            stopLoading();
                        }
                    })
                });
            },
            officeFileIdConfirm(stopLoading, closeDialog) {
                this.$confirm("尚未拟稿，是否确定提交?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    this.submit(stopLoading, closeDialog);
                }).catch(() => {
                    stopLoading();
                });
            },
            submit(stopLoading, closeDialog) {
                let params = this.form;
                taskComplete(this.taskId, params).then(response => {
                    if (response.data.code == 0) {
                        this.$notify({
                            title: '成功',
                            message: '提交成功',
                            type: 'success',
                            duration: 2000
                        });
                        closeDialog();
                        this.goBack();
                    }
                }).finally(() => {
                    stopLoading();
                });
            },
            formValidateErrorNotify() {
                this.$notify({
                    title: '错误',
                    message: '表单存在错误，请按提示修改',
                    type: 'error',
                    duration: 2000
                });
            },
            validateForm(callback) {
                if (this.$refs.form != null) {
                    this.$refs.form.validate((valid, object) => {
                        callback(valid, object);
                    });
                } else {
                    if (callback != null) {
                        callback(true);
                    }
                }
            },
            tabClick(tab) {
                if (tab.name == 'webOffice') {
                    this.$refs.webOfficeButton.clickBtn();
                }
            },
            refreshPreview() {
                this.previewShow = new Date().getTime();
            },
            toWebOfficeCreate() {
                this.webOfficeCreateSpare((fileId) => {
                    this.form.officeFileId = fileId;
                    //拟稿：create   核稿:check  签章：signature
                    WebCtrl.ShowPage(store.getters.access_token, 'create', fileId, null, null, this.list.title, this.list.code);
                });
            },
            webOfficeCreateSpare(callback) {
                webOfficeCreateSpare().then(response => {
                    if (response.data.code == 0) {
                        let fileId = response.data.data;
                        callback(fileId);
                    }
                })
            },
            toWebOfficeCheck() {
                getfileinfo(this.form.officeFileId).then((res) => {
                    let fileInfo = res.data.data;
                    if (fileInfo != null && fileInfo.size > 0) {
                        WebCtrl.ShowPage(store.getters.access_token, 'check', fileInfo.fileId, fileInfo.fileName, null, this.list.title);
                    } else {
                        this.toWebOfficeCreate((fileId) => {
                            this.form.officeFileId = fileId;
                        })
                    }
                });
            },
        }
    }
</script>

<style lang="scss" scoped>
    .collapse {
        margin-top: 2rem;
    }
</style>
